import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../views/Home.vue'
import Users from '../components/users.vue'
import Right from '../components/right.vue'
import Roles from '../components/roles.vue'
import Cate from '../components/cate.vue'
import Params from '../components/params.vue'
import Goods from '../components/goods.vue'
import AddGoods from '../components/addGoods.vue'
import Order from '../components/order.vue'
import Reports from '../components/reports.vue'
Vue.use(VueRouter)

const router = new VueRouter({
  routes:[{
    path:'/',
    redirect:'/login'
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component:Home,
    redirect:'/users',
    children:[{
        path:'/users',
        component:Users
      },{
        path:'/rights',
        component:Right
      },{
        path:'/roles',
        component:Roles
      },{
        path:'/categories',
        component:Cate
      },{
        path:'/params',
        component:Params
      },{
        path:'/goods',
        component:Goods
      },{
        path:'/addGoods',
        component:AddGoods
      },{
        path:'/orders',
        component:Order
      },{
        path:'/reports',
        component:Reports
      }
    ]
 }
]
})
//设置全局路由守卫
router.beforeEach((to,from,next)=>{
  //当用户直接访问登入页时，直接访问
  if(to.path=='/login') return next()
  //取出token值
  const tokenStr=window.sessionStorage.getItem('token');
  //判断token值是否存在  当不存在时 路由返回到登入页
  if(!tokenStr) return next('/login')
  next()
})
export default router
